const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    isContent1Shown: false,
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    reminderList: [
      {
        icon: "/images/fuw.png",
        title: "服务申请进度提醒",
        desc: "医生已经通过了您的签约申请，查看...",
      },
      {
        icon: "/images/fuw.png",
        title: "服务申请进度提醒",
        desc: "医生已经通过了您的签约申请，查看...",
      }
    ],
    hasMore: false,
    teams: [
      {
        id: 1,
        name: '李明明团队',
        location: '翡翠郡社区服务站',
        distance: '4.9',
        rating: 4,
        avatar: '/images/tuan.png'
      },
      {
        id: 2,
        name: '李明明团队',
        location: '翡翠郡社区服务站',
        distance: '4.9',
        rating: 4,
        avatar: '/images/tuan.png'
      },
      {
        id: 3,
        name: '李明明团队',
        location: '翡翠郡社区服务站',
        distance: '4.9',
        rating: 4,
        avatar: '/images/tuan.png'
      },
      {
        id: 4,
        name: '李明明团队',
        location: '翡翠郡社区服务站',
        distance: '4.9',
        rating: 4,
        avatar: '/images/tuan.png'
      },
      {
        id: 5,
        name: '李明明团队',
        location: '翡翠郡社区服务站',
        distance: '4.9',
        rating: 4,
        avatar: '/images/tuan.png'
      }
    ],
    latitude: '',
    longitude: '',
    
    institution:'',
    distances:[],
    jingwei:[],
    institutions: [],
    latitude: 34.818510,
    longitude: 113.561538
  },
  toggleContent: function (e) {
    const isChecked = e.detail.value;
    this.setData({
        isContent1Shown: isChecked
    });
  },

  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    });
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    const { nickName } = this.data.userInfo;
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl!== defaultAvatarUrl,
    });
  },
  onInputChange(e) {
    const nickName = e.detail.value;
    const { avatarUrl } = this.data.userInfo;
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl!== defaultAvatarUrl,
    });
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res);
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      }
    });
  },

  handleSearch(e) {
    const keyword = e.detail;
    console.log('搜索关键词:', keyword);
    // 在这里添加搜索逻辑，比如调用接口进行搜索
  },
  handleCancel() {
    console.log('取消搜索');
    // 在这里添加取消搜索的逻辑，比如清空输入框等
  },
  goToSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },
  // 获取当前位置
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        });
        this.getNearbyTeams();
      },
      fail: () => {
        wx.showToast({
          title: '请开启位置权限',
          icon: 'none'
        });
      }
    });
  },
  onTeamClick(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `./teamDetail/teamDetail?id=${id}`
    });
  },
});